import java.util.Arrays;

public class exercise {
    //字符串去重
    public static String newString(String str){
        StringBuilder ret = new StringBuilder("");
        for(int i = 0; i <str.length(); i++){
            char ch = str.charAt(i);
            //在这里indexOf方法中需要一个字符串，而ch为字符，在后面加上一个"".即可转换为字符串
            if(ret.indexOf(ch + "") < 0){
                ret.append(ch);
            }
        }
        return ret.toString();
    }

    //合并两个有序数组  方法1
    public static String merge(int [] arr1,int [] arr2){
        int len1 = arr1.length;
        int len2 = arr2.length;
        int [] tmp = new int[len1 * 2];
        //将arr1拷贝到临时书数组中
        for(int i = 0; i < len1; i++){
            tmp[i] = arr1[i];
        }
        for(int j = 0; j < len2; j++){
            tmp[len1 + j] = arr2[j];
        }
        Arrays.sort(tmp);
        return Arrays.toString(tmp);
    }

    public static String merge1(int [] A,int [] B){
        int i = B.length - 1;
        int j = B.length - 1;
        int k = B.length*2 - 1;
        while(i >= 0 && j >= 0){
            if(A[i] >= B[j]){
                A[k] = A[i];
                i--;
                k--;
            }else if(A[i] < B[j]){
                A[k] = B[j];
                k--;
                j--;
            }
        }
        while(i >=0){
            A[k] = A[i];
            k--;
            i--;
        }
        while (j >= 0){
            A[k] = A[j];
            k--;
            j--;
        }
        return Arrays.toString(A);
    }

    public static void main(String[] args) {
//        String ret = newString("BabB");
//        System.out.println(ret);
        int [] arr1 = {1,2,3,4,0,0,0,0};
        int [] arr2 = {2,3,4,5};
//        String str = merge(arr1,arr2);
//        System.out.println(str);
        System.out.println(merge1(arr1,arr2));
    }
}
